<?php

require_once("head-foot.php");

function DrawAll($rankingData, $history)
{
	head(WEB_TITLE, null, null);	
	
		BeginContainer();
			DrawColumn1($rankingData["GroupName"]);
			DrawColumn2($rankingData);
			DrawColumn3($history);
		EndContainer();
	
	foot();
}

function DrawColumn1($groupName)
{
	echo "<div class=\"grid_5 left_column\">";
				DrawTitle($groupName);
	echo "</div>";
}

function DrawColumn2($rankingData)
{
	echo "<div class=\"grid_8 center_column\">";
				DrawPrize($rankingData["PrizeInfo"]);
				DrawRanking($rankingData["RankingList"], $rankingData["PrizeInfo"]["RequiredScore"]);
	echo "</div>";
}

function DrawColumn3($history)
{
	echo "<div class=\"grid_5 pull_13 left_column\">";
				DrawHistory($history);
	echo "</div>
		  <div class=\"grid_5 pull_13 left_column\">";
				DrawRules();
    echo "</div>";
}

function DrawTitle($groupName)
{
	echo "<div class=\"title_box\">
			<a href=\"".PROJECT_URL."\"></a>
				<span class=\"group_tag\">".$groupName."</span>
			
		</div>";
}

function DrawHistory($history)
{
	echo "<div class=\"history_box\">
			<span class=\"history_title_tag\">Previous prizes</span>";
		
	DrawHistoryTree($history);
		
	echo	"
		</div>";
}

function DrawHistoryTree($history)
{
	echo "<div id=\"sidetree\" class=\"history_tree\">
			<ul id=\"tree\">";
			
			$currentGroupName = null;
			
			foreach($history as $group)
			{
				if($currentGroupName != $group["GroupName"])
				{
					// Writes list header and footer when the name of the group is different
				
					if($currentGroupName != null)
					{
						echo "</ul>
							</li>";
					}
					
					echo "<li><span class=\"tree_group_tag\">".$group["GroupName"]."</span>
							<ul>";
							
					$currentGroupName = $group["GroupName"];
				}
							
							echo "<li><a href=\"/?prize=".$group["PrizeInfo"]["Id"]."\">".$group["PrizeInfo"]["StartDate"]." - ".$group["PrizeInfo"]["EndDate"]." (".$group["PrizeInfo"]["Name"].")</a></li>";
			}

			if($currentGroupName != null)
			{
				echo "</ul>
					</li>";
			}
			
			echo "</ul>
				</div>";
}

function DrawRules()
{
	echo "<div class=\"rules_box\">
			<a href=\"".RULES_URL."\">You can read the rules of the ranking here</a>
		</div>";
}

function DrawPrize($prizeInfo)
{
	echo "<div class=\"center_column_row1\">
			<div class=\"prize_box\">
				<img src=\"/media/prizes/".$prizeInfo["ImageUrl"]."\" class=\"prize_image_box\" />
			</div>
		</div>
		<div class=\"center_column_row2\">
			<table class=\"prize_description_panel\">
			<tbody>
				<tr>
					<td class=\"points_column\">
						<span class=\"points_for_prize_box\">".$prizeInfo["RequiredScore"]."</span>
					</td>
					<td class=\"info_column\">
						<div class=\"prize_description_box\">
							<span class=\"name_tag\">".$prizeInfo["Name"]."</span> <span class=\"period_tag\">(".$prizeInfo["StartDate"]." - ".$prizeInfo["EndDate"].")</span><br/>
							<span class=\"description_tag\">".$prizeInfo["Description"]."</span><br/>
						</div>
					</td>
					<td class=\"rightside_column\">
						<span class=\"prize_description_rightside\"></span>
					</td>
				</tr>
			</tbody>
			</table>
		</div>";
}

function DrawRanking($rankingList, $requiredScore)
{
	echo "<div class=\"center_column_row3\">
			<div class=\"ranking_list_box\">
				<table class=\"ranking_list\">
				<tbody>";
				foreach($rankingList as $row)
				{
					echo "<tr class=\"ranking_item ".($row["Score"] >= $requiredScore? "highlighted" : "")."\">
						<td class=\"points_column\">
							<span class=\"points_tag\">".$row["Score"]."</span>
						</td>
						<td class=\"info_column\">
							<span class=\"info_tag\">".$row["Name"]."</span>
						</td>
						<td class=\"rightside_column\">
						</td>
					</tr>";
				}
				
				echo "</tbody>
				</table>
			</div>
		</div>";
}

function BeginContainer()
{
	echo "<body class=\"main_background\">

			<div class=\"container clearfix\">";
}

function EndContainer()
{
	echo "</div>";
}

?>